import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {MyDateModel} from '../../../../models/my-date.model';
import {BaseService} from '../../../../shared/service/base.service';

@Component({
  selector: 'app-log-clear',
  templateUrl: './log-clear.component.html',
  styleUrls: ['./log-clear.component.less']
})
export class LogClearComponent implements OnInit{

  // 模态框显示
  @Input()
  isVisible = false;
  // 组件传值
  @Output()
  // 父子组件传值
  event: EventEmitter<any> = new EventEmitter();
  // 删除时间段
  radioValue = '1'
  // 开始日期类型
  startDateTimeType: string = 'start';
  // 结束日期类型
  endDateTimeType: string = 'end';
  // 开始日期
  startDateTime: Date;
  // 结束日期
  endDateTime: Date;

  constructor(public baseService: BaseService) {
  }

  ngOnInit(): void {

    /**
     * 拿到当前日期，然后拼接00:00:00或23:59:59 begin
     */
    let dateTime = new Date();
    let year = dateTime.getFullYear();
    let month = dateTime.getMonth() + 1;
    let day = dateTime.getDate();
    let startDateTimeStr = year + "/" + month + "/" + day + " 00:00:00";
    let endDateTimeStr = year + "/" + month + "/" + day + " 23:59:59";

    this.startDateTime = new Date(startDateTimeStr);
    this.endDateTime = new Date(endDateTimeStr);
    /**
     * 拿到当前日期，然后拼接00:00:00或23:59:59 end
     */
  }

  // 按时间段删除成功时要关闭模态框
  deleteInDateTimeSuccess(){
    this.isVisible = false;
    let body = {'isVisible': false};
    this.event.emit(body);
  }

  // 确定修改
  handleOk(): void {
    let msg = '';
    let startDateTime = this.startDateTime;
    let endDateTime = this.endDateTime;
    if (this.radioValue == '1'){
      // 半年前
      startDateTime = null;
      msg = '半年前';
      endDateTime = this.baseService.zorro.getDateTime().sixMonthsAgo;
    } else if (this.radioValue == '2'){
      // 3个月前
      startDateTime = null;
      msg = '3个月前';
      endDateTime = this.baseService.zorro.getDateTime().threeMonthsAgo;
    } else if (this.radioValue == '3'){
      // 1个月前
      startDateTime = null;
      msg = '1个月前';
      endDateTime = this.baseService.zorro.getDateTime().oneMonthsAgo;
    } else {
      // 自定义
      startDateTime = this.startDateTime;
      endDateTime = this.endDateTime;
      msg = this.baseService.zorro.getFormatDateTime(this.startDateTime, 'yyyy-MM-dd HH:mm:ss') + '至' + this.baseService.zorro.getFormatDateTime(this.endDateTime, 'yyyy-MM-dd HH:mm:ss');
    }
    let body = {}
    this.baseService.layer.modalConfirm('确定要删除' + msg + '的记录吗?', result => {
      if (result) {
        body = {
          'startDateTime': startDateTime,
          'endDateTime': endDateTime
        };
        this.event.emit(body);
      }
    });
  }

  // 取消修改
  handleCancel(): void {
    this.isVisible = false;
    let body = {'cancel': true, 'isVisible': this.isVisible};
    this.event.emit(body);
  }

  // 将开始、结束时间往外抛
  outDateTime($event: MyDateModel) {
    /**
     * date: Fri Mar 19 2021 23:59:06 GMT+0800 (中国标准时间) {}
     * type: "end"
     */
    if ($event.type == 'start'){
      this.startDateTime = $event.date;
    } else if ($event.type == 'end'){
      this.endDateTime = $event.date;
    }
  }
}
